package cn.powertime.evaluation.config;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author ZYW
 * @version v1.0.0
 * @date 2019-09-02 16:31
 */
@Configuration
@EnableSwagger2
@Profile({"local","dev","test"})
public class Swagger2Config {
    @Bean
    public Docket createRestApiWeb() {
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter = builder
                .parameterType("cookie") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("") //默认值
                .description("请输入token")
                .modelRef(new ModelRef("string")) //指定参数值的类型
                .required(false).build(); //非必需，这里是全局配置，然而在登陆的时候是不用验证的
        List<Parameter> parameters = Lists.newArrayList(parameter);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("pc")
                .enable(true)
                .produces(Sets.newHashSet("application/json","application/x-www-form-urlencoded","*/*"))
                .consumes(Sets.newHashSet("application/json","application/x-www-form-urlencoded","*/*"))
                .protocols(Sets.newHashSet("http"))
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.powertime.evaluation"))
                .paths(PathSelectors.regex(".*/(web|auth|file)/.*"))
                .build()
                .apiInfo(this.apiInfo())
                .globalOperationParameters(parameters);
    }

    @Bean
    public Docket createRestApiApp() {
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter = builder
                .parameterType("cookie") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("") //默认值
                .description("请输入token")
                .modelRef(new ModelRef("string")) //指定参数值的类型
                .required(false).build(); //非必需，这里是全局配置，然而在登陆的时候是不用验证的
        List<Parameter> parameters = Lists.newArrayList(parameter);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("app")
                .enable(true)
                .produces(Sets.newHashSet("application/json","application/x-www-form-urlencoded","*/*"))
                .consumes(Sets.newHashSet("application/json","application/x-www-form-urlencoded","*/*"))
                .protocols(Sets.newHashSet("http"))
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.powertime.evaluation"))
                .paths(PathSelectors.regex(".*/app/.*"))
                .build()
                .apiInfo(this.apiInfo())
                .globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Powertime Restful APIs")
                .description("服务端后台接口说明文档")
                .version("1.0")
                .build();
    }
}
